package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.dao.CdnHouseInfoLinkDao;

@Repository
public class CdnHouseInfoLinkDaoImpl implements CdnHouseInfoLinkDao {

	private static final Log log = LogFactory.getLog(CdnDataDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(IMap param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select (select cdn.cdnName from t_cdn_cdndata cdn where cdn.cdnId = t.cdnId) as cdnName,t.houseId,t.houseName,t.houseAdd,");
		sql.append("       (select area.province from provinces area  where area.provinceid = t.houseProvince limit 1) as houseProvince,");
		sql.append("       case when t.houseType = '1' then '租用' when t.houseType = '2' then '自建' when t.houseType = '999' then '其他' end as houseType");
		sql.append("  from t_cdn_cdnNet_node_house t where 1 = 1");
		sql.append(getQueryConf(param));
		sql.append(" group by t.houseId,t.houseName,t.houseAdd,t.houseType");
		
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count from(");
		sql.append(" select 1 from t_cdn_cdnNet_node_house t where 1 = 1");
		sql.append(getQueryConf(param));
		sql.append(" group by t.houseId,t.houseName,t.houseAdd,t.houseType");
		sql.append(") t");
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(IMap param) {
		String houseName = CommonUtils.valueOf(param.get("houseName"));
		String houseType = CommonUtils.valueOf(param.get("houseType"));
		String linkCdnId = CommonUtils.valueOf(param.get("linkCdnId"));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.equals(userLevel, "1")) {//TODO
			queryConf.append(" and t.houseProvince = (select pro.provinceid from provinces pro where pro.province = '"+userProvince+"' limit 1)");
		}
		if(StringUtils.isNotBlank(houseName)) {
			queryConf.append(" and t.houseName like '%"+houseName+"%'");
		}
		if(StringUtils.isNotBlank(houseType)) {
			queryConf.append(" and t.houseType = '"+houseType+"'");
		}
		if(StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and t.cdnId='"+linkCdnId+"'");
		}
		if(StringUtils.isNotBlank(linkCdnNetId)) {
			queryConf.append(" and t.cdnNetId='"+linkCdnNetId+"'");
		}
		return queryConf;
	}
}